/*
 * SMMS的login类：
 * 主要负责登陆的验证
 */
package com.res0w.SMMS.login;

import com.res0w.SMMS.factory.DataBaseOperate;
import com.res0w.SMMS.factory.SHA1Encoding;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author res0w
 * @since 2009-9-21
 * @version 0.2
 */
public class LoginAuth {

    public LoginAuth() {
    }

    public int login(String usrname, String pwd) throws SQLException {
        int result = -1;
        String sql = "select pwd,accountType from admininfo where usrname='" + usrname + "'";
        ResultSet rs = null;
        DataBaseOperate db = new DataBaseOperate();
        rs = db.dbQueryMuti(sql);
        while (rs.next()) {
            try {
                String pwdEncoding = SHA1Encoding.SHA1(pwd);
                if (pwdEncoding.equals(rs.getString(1).toString().trim())) {
                    result = Integer.parseInt(rs.getString(2));
                } else {
                    result = -1;
                }
            } catch (NoSuchAlgorithmException ex) {
                Logger.getLogger(LoginAuth.class.getName()).log(Level.SEVERE, null, ex);
            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(LoginAuth.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return result;
    }

    public boolean login(String usrname, String pwd, int accountType) throws SQLException {
        boolean result = false;
        try {
            String sql = "select pwd from admininfo where usrname='" + usrname + "'";
            ArrayList list = new ArrayList();
            DataBaseOperate db = new DataBaseOperate();
            list = db.dbQuerySolo(sql, true);
            String pwdEncoding = SHA1Encoding.SHA1(pwd);
            if (pwdEncoding.equals(list.get(0).toString().trim())) {
                result = true;
            }
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(LoginAuth.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(LoginAuth.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }
    /*
     * deprecated
    //0:管理员帐户；1：一般账户
    private void selectUI(int index) {

    switch (index) {
    case 0:
    case 1:
    }
    }
     */

    public static void main(String[] args) throws SQLException {
    LoginAuth lg =new LoginAuth();
    System.out.println(lg.login("admin", "881010"));
    }
     
}
